# -*- coding: utf-8 -*-

import wx
import regist
import re
class KeyGenApp(wx.App):
    def OnInit(self):
        frame = KeyGenFrame()
        frame.Show()
        return True

class KeyGenFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, u"Crawler注册码生成器", size=(350,250), pos= (200,200), style = wx.CAPTION|wx.NO_BORDER|wx.SYSTEM_MENU|wx.CLOSE_BOX)
        self.width = 350
        self.height = 250
        self.panel = wx.Panel(self, -1)
        self.createBoxes(self.panel)
        self.createButtonBar(self.panel)
        self.CreateStatusBar()
    
    def createBoxes(self, parent):
        wx.StaticText(parent, -1, u'机器码:', pos = (30, 50))
        wx.StaticText(parent, -1, u'注册码:', pos = (30, 90))
        self.codearea = wx.TextCtrl(parent, -1, u'',pos = (100,90),size=(200, -1))
        self.mcarea =  wx.TextCtrl(parent, -1, u'请输入机器码',pos = (100,50),size=(200, -1))

    def createButtonBar(self, parent):
        self.startButton = wx.Button(parent,-1,u'生成', pos = (self.width/2 -40,self.height - 100))
        self.Bind(wx.EVT_BUTTON, self.OnStart, self.startButton)
        
    
    def OnStart(self, event):
        mc = self.mcarea.GetString(0, 100).strip()
        if not re.match('([0-9,A-F]{4}-[0-9,A-F]{4}-[0-9,A-F]{4}-[0-9,A-F]{4})', mc):
            wx.MessageBox( u'错误的机器码格式，正确的格式为 xxxx-xxxx-xxxx-xxxx',u'格式错误',wx.OK)
            return
        else:
            self.codearea.Clear()
            self.codearea.WriteText(regist.calccode(mc))
    
if __name__ == '__main__':
    app = KeyGenApp()
    app.MainLoop()